/**
 * @program: LeetCode
 * @description: LeetCode : 剑指 Offer II 047. 二叉树剪枝
 * @author: WXY
 * @create: 2023-01-02 10:05
 * @Version 1.0
 **/
public class offer_sixteen1_pruneTree {
    public static TreeNode pruneTree(TreeNode root) {
        boolean process = process(root);
        return process ? null : root;
    }

    private static boolean process(TreeNode root) {
        if (root == null) return false;
        boolean leftFlag = process(root.left);
        if (leftFlag) root.left = null;
        boolean rightFlag = process(root.right);
        if (rightFlag) root.right = null;
        if (root.val == 0 && root.left == null && root.right == null) {
            return true;
        }
        return false;
    }

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
